package com.ruoyi.parkinglot.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.parkinglot.service.ParkLaneService;
import com.ruoyi.parkinglot.utils.Result;
import com.ruoyi.parkinglot.domain.ParkLane;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

@RestController
@RequestMapping("/ParkLane")
public class ParkLaneController {
    private static final Logger log = LoggerFactory.getLogger(ParkLaneController.class);

    @Resource
    private ParkLaneService parkLaneService;

    /**
     * 查询车道列表
     */
    @GetMapping("/list")
    public Result list(ParkLane parkLane,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            startPage(pageNum, pageSize);
            List<ParkLane> list = parkLaneService.selectParkLaneList(parkLane);
            PageInfo<ParkLane> pageInfo = new PageInfo<>(list);

            // 打印返回数据的详细信息
            log.info("查询结果 - 总条数: {}, 当前页数据量: {}", pageInfo.getTotal(), list.size());

            // 构建返回结果
            JSONObject result = new JSONObject();
            result.put("total", pageInfo.getTotal());
            result.put("rows", list);
            result.put("pageNum", pageInfo.getPageNum());
            result.put("pageSize", pageInfo.getPageSize());

            return Result.success(result);
        } catch (Exception e) {
            log.error("查询车道列表失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取车道详细信息
     */
    @GetMapping("/Anparklane/{laneId}")
    public Result getInfo(@PathVariable Long laneId) {
        return Result.success(parkLaneService.getById(laneId));
    }

    /**
     * 新增车道
     */
    @PostMapping("AddParkLane")
    public Result add(@RequestBody ParkLane parkLane) {
        return Result.success(parkLaneService.save(parkLane));
    }

    /**
     * 修改车道
     */
    @PutMapping("/UPParkLane")
    public Result edit(@RequestBody ParkLane parkLane) {
        return Result.success(parkLaneService.updateById(parkLane));
    }

    /**
     * 删除车道
     */
    @DeleteMapping("/DeParkLane/{laneId}")
    public Result remove(@PathVariable Long laneId) {
        return Result.success(parkLaneService.removeById(laneId));
    }

    /**
     * 生成车道二维码
     */
    @GetMapping("/qrcode/{laneId}")
    public Result generateQrCode(@PathVariable Long laneId) {
        String qrCode = parkLaneService.generateQrCode(laneId);
        return Result.success(qrCode);
    }
}
